package com.zhengtoudai.ui.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.zhengtoudai.R;
import com.zhengtoudai.base.MvpActivity;
import com.zhengtoudai.global.Config;
import com.zhengtoudai.ui.bean.ShareRecordModel;
import com.zhengtoudai.ui.presenter.ShareRecordPresenter;
import com.zhengtoudai.ui.view.ShareRecordView;
import com.zhengtoudai.util.AbToastUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

/**
 *
 */
public class MyShareActivity extends MvpActivity<ShareRecordPresenter> implements ShareRecordView {

    @Bind(R.id.iv_eeweima)
    ImageView mIvEeweima;
    @Bind(R.id.btn_affirm)
    Button mBtnAffirm;

    private ShareRecordModel.ShareRecordBean inviteRecord;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_share);
        ButterKnife.bind(this);
    }

    @Override
    protected ShareRecordPresenter createPresenter() {
        return new ShareRecordPresenter(this);
    }

    @Override
    protected void initTitle() {
        titleUtil.setTitleOrRight(true, "我的分享", "邀请记录", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(MyShareRecordActivity.class);
            }
        });
    }

    @Override
    protected void initView() {
        mIvEeweima.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {

                return false;
            }
        });
        mvpPresenter.loadData(context,"1", "1");
    }

    @OnClick(R.id.btn_affirm)
    public void onClick() {

        if (inviteRecord == null) {
            AbToastUtil.showToast(context, "未获取到分享信息，请稍后重试");
            return;
        }
        showShare();
    }

    private void showShare() {
        ShareSDK.initSDK(this);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间等使用
        oks.setTitle(inviteRecord.getShareContentTitle());
        // titleUrl是标题的网络链接，QQ和QQ空间等使用
        oks.setTitleUrl(inviteRecord.getShareContentLinkurl());
        // text是分享文本，所有平台都需要这个字段
        oks.setText(inviteRecord.getShareContentContent());
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        File img = loadLocalImg();
        if (img != null) {
            oks.setImagePath(img.getPath());
        }
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl(inviteRecord.getShareContentLinkurl());
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl(inviteRecord.getShareContentLinkurl());

        // 启动分享GUI
        oks.show(this);

    }

    private File loadLocalImg() {
        File img = new File(Environment.getExternalStorageDirectory(), Config.base_data_path + "/share.png");
        //存在，则直接返回
        if (img.exists()) {
            return img;
        }
        //不存在则创建
        try {
            img.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.app_icon);
        try {
            FileOutputStream fos = new FileOutputStream(img);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
            return img;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void getDataSuccess(ShareRecordModel model) {
        if (model.getCode().equals("200")) {
            inviteRecord = model.getResult();
            Glide.with(this)
                    .load(inviteRecord.getRecommendPth())
                    .error(R.mipmap.erweima)
                    .into(mIvEeweima);
        } else {
            AbToastUtil.showToast(context, model.getMessage());
        }
    }

    @Override
    public void getDataFail(int code) {

    }

    @Override
    public void showLoading() {
        showProgressDialog("正在获取数据...");
    }

    @Override
    public void hideLoading() {
        dismissProgressDialog();
    }
}
